// import { getOrganizationTree } from '@/api/sys/org';
import { stoporstartUser } from '@/api/sys/user'
import { BasicColumn, FormSchema } from '@/components/Table'
import { useMessage } from '@/hooks/web/useMessage'
import { Switch } from 'ant-design-vue'
import { h } from 'vue'
import { forEach } from '@/utils/helper/treeHelper'

type CheckedType = boolean | string | number

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '账号',
    dataIndex: 'phone',
  },
  {
    title: '角色',
    dataIndex: 'role',
    customRender: ({ record }) => {
      if (record.roleVoList) {
        const arr = record.roleVoList.map((item: any) => item.roleName)
        return arr.join(',')
      }
      return null
    },
  },
  // {
  //   title: '用户状态',
  //   dataIndex: 'status',
  //   width: 120,
  //   customRender: ({ record }) => {
  //     if (!Reflect.has(record, 'pendingStatus')) {
  //       record.pendingStatus = false
  //     }
  //     return h(Switch, {
  //       checked: record.status === 0,
  //       checkedChildren: '启用',
  //       unCheckedChildren: '停用',
  //       loading: record.pendingStatus,
  //       onChange(checked: CheckedType) {
  //         record.pendingStatus = true
  //         const newStatus = checked ? 0 : 1
  //         const { createMessage } = useMessage()
  //         stoporstartUser({ ...record, status: newStatus })
  //           .then(() => {
  //             record.status = newStatus
  //             // createMessage.success(`已成功修改用户状态`)
  //           })
  //           .catch(() => {
  //             createMessage.error('修改用户状态失败')
  //           })
  //           .finally(() => {
  //             record.pendingStatus = false
  //           })
  //       },
  //     })
  //   },
  // },
  {
    title: '邮箱',
    dataIndex: 'email',
  },
  {
    title: '所属组织',
    dataIndex: 'orgName',
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'orgName',
    label: '所属组织',
    component: 'Input',
    colProps: { span: 6 },
  },
]

export const accountFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    // helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      // {
      //   trigger: 'blur',
      //   validator(_, value) {
      //     return new Promise((resolve, reject) => {
      //       if (!value) return resolve();
      //       isAccountExist(value)
      //         .then(resolve)
      //         .catch((err) => {
      //           reject(err.message || '验证失败');
      //         });
      //     });
      //   },
      // },
    ],
  },
  {
    field: 'phone',
    label: '账号',
    component: 'Input',
    required: true,
    // ifShow: false,
  },
  {
    field: 'orgId',
    label: '所属组织',
    required: true,
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'name',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    // required: true,
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    // required: true,
  },
]

export const changePasswordSchema: FormSchema[] = [
  {
    field: 'passwordOld',
    label: '当前密码',
    component: 'InputPassword',
    required: true,
  },
  {
    field: 'passwordNew',
    label: '新密码',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '新密码',
    },
    rules: [
      {
        required: true,
        message: '请输入新密码',
      },
    ],
  },
  {
    field: 'confirmPassword',
    label: '确认密码',
    component: 'InputPassword',
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (!value) {
              return Promise.reject('密码不能为空')
            }
            if (value !== values.passwordNew) {
              return Promise.reject('两次输入的密码不一致!')
            }
            return Promise.resolve()
          },
        },
      ]
    },
  },
]
